package com.yoo.ons.function

import androidx.lifecycle.MutableLiveData
import com.framework.network.remote.NetworkObserver
import com.framework.network.remote.endRequest
import com.framework.network.remote.error
import com.framework.network.remote.requestObserver
import com.framework.network.remote.startRequest
import com.framework.network.remote.success
import com.yoo.ons.model.IMTokenModel
import com.yoo.ons.model.PolicyModel
import com.yoo.ons.model.ServerUserInfoModel
import com.yoo.ons.model.UserInfoBaseModel
import com.yoo.ons.base.BaseVMViewModel
import com.yoo.ons.base.HttpRequestListener
import com.yoo.ons.base.Request
import com.yoo.ons.model.ConfigsEquityModel
import com.yoo.ons.model.ConversationInfoModel
import com.yoo.ons.model.UserInfoModel

class MainViewModel : BaseVMViewModel() {

    val showTabLiveData = MutableLiveData<Boolean>()
    val cardSlideState = MutableLiveData<Boolean>()
    val clickMenuType = MutableLiveData<Int>()
    val userInfoLiveData = MutableLiveData<UserInfoModel>()
    //隐藏显示tab
    fun showMainTab(isShow:Boolean){
        showTabLiveData.value = isShow
    }
    //卡片是否滑动
    fun slideCardState(isSlide:Boolean){
        cardSlideState.value = isSlide
    }
    //点击弹窗底部按钮发送类型
    fun clickMenuType(type:Int){
        clickMenuType.value = type
    }

    fun getCheckLogOut(listener: HttpRequestListener<String>) {
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postCheckLogOut(map)?.requestObserver(
                    NetworkObserver<String>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        requestEndListener?.invoke()
                    }

                )
            }

        })

    }

    /**
     * 查询用户信息
     */
    fun getUserInfo(listener: HttpRequestListener<UserInfoModel>){
        request(listener,request=object :Request{
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postUserInfo(map)?.requestObserver(
                    NetworkObserver<UserInfoModel>().startRequest {
                    }.success {
                        userInfoLiveData.value = it
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        requestEndListener?.invoke()
                    }
                )
            }

        })

    }

    /**
     * 获取融云
     */
    fun getIMToken(listener: HttpRequestListener<IMTokenModel>) {
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postIMToken(map)?.requestObserver(
                    NetworkObserver<IMTokenModel>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        requestEndListener?.invoke()
                    }
                )

            }

        })
    }

    /**
     * 获取用户基本信息
     */
    fun getUserInfoBase(listener: HttpRequestListener<UserInfoBaseModel>) {
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postUserInfoBase(map)?.requestObserver(
                    NetworkObserver<UserInfoBaseModel>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        requestEndListener?.invoke()
                    }
                )

            }

        })
    }

    /**
     * 获取客服信息
     */
    fun getServer(listener: HttpRequestListener<List<ServerUserInfoModel>>){
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postServerUserInfo(map)?.requestObserver(
                    NetworkObserver<List<ServerUserInfoModel>>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        requestEndListener?.invoke()
                    }
                )

            }

        })
    }

    /**
     * 获取PPV开关
     */
    fun getEquityConfig(listener: HttpRequestListener<ConfigsEquityModel>){
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postEquityConfig(map)?.requestObserver(
                    NetworkObserver<ConfigsEquityModel>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        requestEndListener?.invoke()
                    }
                )

            }

        })
    }

    /**
     * 上传设备信息
     */
    fun getAddDeviceInfo(listener: HttpRequestListener<String>){
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postAddDeviceInfo(map)?.requestObserver(
                    NetworkObserver<String>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        requestEndListener?.invoke()
                    }
                )

            }

        })
    }

    /**
     * 获取用户列表的标识
     */
    fun getConversationList(listener: HttpRequestListener<List<ConversationInfoModel>>){
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postConversationList(map)?.requestObserver(
                    NetworkObserver<List<ConversationInfoModel>>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        requestEndListener?.invoke()
                    }
                )

            }

        })
    }

}